package com.orkhonsoft.tengrinshar.chat.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.orkhonsoft.tengrinshar.chat.common.PageUtils;
import com.orkhonsoft.tengrinshar.chat.common.Query;
import com.orkhonsoft.tengrinshar.chat.common.RestCode;
import com.orkhonsoft.tengrinshar.chat.common.RestResponse;
import com.orkhonsoft.tengrinshar.chat.entity.FriendEntity;
import com.orkhonsoft.tengrinshar.chat.entity.UserEntity;
import com.orkhonsoft.tengrinshar.chat.service.FriendService;
import com.orkhonsoft.tengrinshar.chat.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;


/**
 * 好友
 * 
 * @author bain
 * @email 2668013653@qq.com
 * @date 2018-10-29 10:59:17
 */
@Api("好友相关的相关的api")
@RestController
@RequestMapping("friend")
public class FriendController extends AbstractController{
	@Autowired
	private FriendService friendService;
	@Autowired
	private UserService userService;
	/**
	 * 查询本人好友列表
	 * @return
	 */
	@ApiOperation(value = " 查询本人好友列表", notes = " 删除好友申请")
	@GetMapping("list")
	public RestResponse<List<FriendEntity>> list() {
		List<FriendEntity> list = friendService.queryListByUser(getUserId());
		friendService.setFriend(list);

		return RestResponse.success(list);
	}
	/**
	 * 查看好友信息
	 * @param id
	 * @return
	 */
	@ApiOperation(value = " 查看好友信息", notes = " 查看好友信息")
	@ApiImplicitParam(name = "id", value = "好友ID", paramType = "path", required = true, dataType = "Long")
	@GetMapping("/{id}")
	public RestResponse<FriendEntity> show(@PathVariable("id") Long id) {
		Long userId = getUserId();
		FriendEntity friend = friendService.queryObject(id);

		if(friend == null || !friend.getUserId().equals(userId)) {
			return RestResponse.error(RestCode.DATA_NOT_FOUND);
		}
		
		return RestResponse.success(friend);
	}
	/**
	 * 添加好友
	 * @param friend
	 * @return
	 */
	/*@ApiOperation(value = "添加好友", notes = "添加好友")
	@ApiImplicitParam(name = "friend", value = "FriendEntity对象", paramType = "path", required = true, dataType = "FriendEntity实体类")
	@PostMapping("addFriend")
	public RestResponse<Object> save(FriendEntity friend) {
		friendService.save(friend);
		
		return RestResponse.success();
	}*/
	/**
	 * 更新好友信息
	 * @param id
	 * @param friend
	 * @return
	 */
	@ApiOperation(value = "更新好友信息", notes = "更新好友信息")
	@PutMapping("/{id}")
	public RestResponse<Object> update(@PathVariable("id") Long id, FriendEntity friend) {
		Long userId = getUserId();
		FriendEntity orgFriend = friendService.queryObject(id);
		
		if(orgFriend == null || !orgFriend.getUserId().equals(userId)) {
			return RestResponse.error(RestCode.DATA_NOT_FOUND);
		}
		
		friend.setId(id);
		friendService.update(friend);
		
		return RestResponse.success();
	}
	/**
	 * 删除好友
	 * @param id
	 * @return
	 */
	@DeleteMapping("/{id}")
	public RestResponse<Object> delete(@PathVariable("id") Long id) {
		Long userId = getUserId();
		FriendEntity friend = friendService.queryObject(id);
		
		if(friend == null || !friend.getUserId().equals(userId)) {
			return RestResponse.error(RestCode.DATA_NOT_FOUND);
		}
		
		friendService.delete(id);
		
		return RestResponse.success();
	}

	@ApiOperation(value = " 搜索好友", notes = " 搜索好友")
	@ApiImplicitParam(name = "mobile", value = "手机号", paramType = "mobile", required = true, dataType = "string")
	@GetMapping("/getFriendBymobile")
	public RestResponse<UserEntity> getFriendByPhone(@RequestParam String mobile) {
		UserEntity userEntity=userService.getFriendByPhone(mobile);
		
		
		return RestResponse.success(userEntity);
	}

	@ApiOperation(value = "查看是否是好友")
	@ApiImplicitParam(name = "userId", value = "好友ID", required = true, dataType = "Long")
	@GetMapping("/{userId}/exist")
	public RestResponse<Boolean> getFriendInfo(@PathVariable("userId") Long userId) {
		FriendEntity friendEntity = new FriendEntity();
		friendEntity.setFriendUserId(userId);
		friendEntity.setUserId(getUserId());

		FriendEntity friend = friendService.queryByFriend(friendEntity);

		Boolean exist = friend != null;
		return RestResponse.success(exist);

	}
	
}
